package com.fc.campusapplicationsystem.controller;

import com.fc.campusapplicationsystem.model.Reply;
import com.fc.campusapplicationsystem.model.Serviceman;
import com.fc.campusapplicationsystem.service.ReplyService;
import com.fc.campusapplicationsystem.util.ResponseOBJ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author kyc
 * @2020-06-10 15:28
 */
@RestController
@RequestMapping("/replyController")
public class ReplyController {

    @Autowired
    ReplyService replyService;

    @GetMapping("/list")
    public ResponseOBJ<List<Reply>> list() {
        ResponseOBJ<List<Reply>> responseOBJ = ResponseOBJ.getInstance();
        try {
            List<Reply> replyList = replyService.list();
            responseOBJ.setData(replyList);
            responseOBJ.setMsg("查询成功");
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            responseOBJ.setMsg("查询失败");
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }

    /**
     * 获得对当前报修单的问题回复
     * @param reId 报修单id
     * @return 保修单
     */
    @GetMapping("/getReplyByReListId")
    public ResponseOBJ<Reply> getReplyByReListId(@RequestParam("reId") Integer reId){
        ResponseOBJ<Reply> responseOBJ = ResponseOBJ.getInstance();
        try {
            Reply reply = replyService.getReplyByReListId(reId);
            responseOBJ.setData(reply);
            responseOBJ.setMsg("查询成功");
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            responseOBJ.setMsg("查询失败");
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }


    /**
     * @param reply 回复内容
     * @param request  session
     * @return 成功
     */
    @PostMapping("/add")
    public ResponseOBJ<Integer> add(@RequestBody Reply reply, HttpServletRequest request) {
        ResponseOBJ<Integer> responseOBJ = ResponseOBJ.getInstance();
        HttpSession session = request.getSession(true);
        Serviceman serviceman = (Serviceman) session.getAttribute("serviceman");
        reply.setServiceId(serviceman.getId());
        try {
            Integer result = replyService.add(reply);
            responseOBJ.setData(result);
            responseOBJ.setCode(ResponseOBJ.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            responseOBJ.setCode(ResponseOBJ.ERROR);
        }
        return responseOBJ;
    }
}
